<?php


/* SVN FILE: $Id: menu.php 107 2007-04-14 17:07:09Z Andy $ */

/**
 * Menu data generation component
 *
 * automatically sets data to the view to be used for the menu elements
 *
 * Copyright (c), Andy Dawson
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright    Copyright (c) 2007, Andy Dawson
 * @package      noswad
 * @subpackage   generic.controllers.components
 * @since        Noswad site version 3
 * @todo         TODO Description still a bit brief :)
 * @version      $Revision: 107 $
 * @created      26/01/2007
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2007-04-14 19:07:09 +0200 (sáb, 14 abr 2007) $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Short description for Class
 *
 * Long descrption of Class
 */
class MenuComponent extends Object {
	var $name= 'Menu';

	/**
	 * Data for all main menus
	 *
	 * @var array
	 * @access private
	 */
	var $__mainMenus= array ();

	/**
	 * Data for all context menus
	 *
	 * @var type
	 * @access private
	 */
	var $__contextMenus= array ();

	/**
	 * Description for var
	 *
	 * @var type
	 * @access public/private/protected
	 */
	var $excludedControllers= array ();

	/**
	 * Description for var
	 *
	 * @var type
	 * @access public/private/protected
	 */
	var $excludedActions= array (
		'index',
		'delete',
		'edit',
		'view',
		'css',
		'js',
		'admin_index'
	);
	/**
	 * Description for var
	 *
	 * @var type
	 * @access public/private/protected
	 */
	var $siteAdmin= 'you';
	/**
	 * Startup - Link the component to the controller.
	 *
	 * @param object $controller
	 * @return void
	 */
	function startup(& $controller) {
		$this->controller= & $controller;
		if ($controller->plugin) {
			$this->excludedControllers[]= $controller->plugin;
		}
	}

	/**
	 * Determins and sets the menu vars to the view
	 *
	 * @return void
	 */
	function setVars() {
		$this->__topLevel();

		if (DEBUG) {
			$adminActions = $this->_getAdminContext();
			if($adminActions['data']) {
				$this->__contextMenus['Admin Actions']= $adminActions;
				$this->__contextMenus['Admin Actions']['sequence']= -9999;
			}
		}
		$this->_defineTopMenus();
		$this->_defineContextMenus();
		$this->controller->set('mainMenus', $this->__mainMenus);
		$existing= isset ($this->controller->viewVars['contextMenus']) ? $this->controller->viewVars['contextMenus'] : array ();
		$this->controller->set('contextMenus', am($this->__contextMenus, $existing));
	}

	function __topLevel() {
		$item= array (
			'title' => 'Home',
			'url' => '/'
		);
		if (!isset ($this->controller->plugin)) {
			$item['active']= true;
		}
		$this->__mainMenus[] = $item;
		uses('Folder');
		$Folder= new Folder(APP . 'plugins');
		list ($Folders)= $Folder->ls();
		foreach ($Folders as $plugin) {
			if ($plugin <> '.svn') {
				$item= array (
					'title' => Inflector :: Humanize($plugin
				), 'url' => '/' . $plugin);
				if (isset ($this->controller->plugin) && ($this->controller->plugin == $plugin)) {
					$item['active']= true;
				}
				$this->__mainMenus[] = $item;
			}
		}
	}

	/**
	 * Define which other controllers are available based on the logged in users
	 * rights
	 *
	 * @return void
	 */
	function __getControllers() {
		if ($this->controller->plugin) {
			$Plugin= Inflector :: camelize($this->controller->plugin);
			$FileList= listClasses(APP . 'plugins' . DS . $this->controller->plugin . DS . 'controllers');
			$excludeList= $this->excludedControllers;
			foreach ($FileList as $file) {
				$list= explode('_', $file);
				array_pop($list);
				$controller= implode($list, '_');
				if (!in_array(low($controller), $excludeList)) {
					$array= array (
						'title' => Inflector :: humanize($controller
					), 'url' => '/' . $Plugin . '/' . Inflector :: camelize($controller));
					if (Inflector :: underscore($this->controller->name) == $controller) {
						$array['active']= true;
					}
					$menu[]= $array;
				}
			}
		} else {
			$Files= listClasses(APP . 'controllers');
			foreach ($Files as $file) {
				$list= explode('_', $file);
				array_pop($list);
				$controller= implode($list, '_');
				$array= array (
					'title' => Inflector :: humanize($controller
				), 'url' => '/' . Inflector :: camelize($controller));
				if (Inflector :: underscore($this->controller->name) == $controller) {
					$array['active']= true;
				}
				$menu[]= $array;
			}
		}
		return array (
			$menu
		);
	}

	/**
	 * Add an admin menu if the current logged in user is permitted
	 *
	 * @return void
	 */
	function _getAdminContext() {
		$excludeList= array (
			'view',
			'edit',
			'delete',
			'index'
		);
		$class_methods= get_class_methods($this->controller);
		$controller_methods= get_class_methods('Controller');
		$controller_methods= array_diff($class_methods, $controller_methods);

		if ($this->controller->plugin) {
			$plugin= Inflector :: camelize($this->controller->plugin);
		} else {
			$plugin= false;
		}
		$return= array ();
		foreach ($controller_methods as $method) {
			$admin= strpos($method, CAKE_ADMIN);
			$method= r(CAKE_ADMIN . '_', '', $method);
			if (($admin !== false) && (!in_array(low($method), $excludeList))) {
				$url= '/' . CAKE_ADMIN . '/';
				$url .= $plugin ? $plugin . '/' : '';
				$url .= Inflector :: camelize($this->controller->name);
				$url .= '/' . $method;
				$urlArray= array (
					'title' => Inflector :: humanize($method
				), 'url' => $url);
				if (low($this->controller->action) == low($method)) {
					$urlArray['active']= true;
				}
				$return[]= $urlArray;
			}
		}
		return array (
			'data' => $return
		);
	}

	/**
	 * Define the main menu based on the current users rights
	 *
	 * @return void
	 */
	function _defineTopMenus() {
		$mainMenus= '';
		if (method_exists($this->controller, '_defineTopMenus')) {
			$mainMenus= $this->controller->_defineTopMenus();
		}
		elseif (isset ($this->controller->plugin)) {
			$mainMenus= $this->__getControllers();
		}
		$this->__mainMenus= $mainMenus;
	}

	/**
	 * Define which methods are available based on the logged in users
	 * rights
	 *
	 * @return void
	 */
	function _defineContextMenus() {
		$ContextMenus= array ();
		if (method_exists($this->controller, '_defineContextMenus')) {
			$ContextMenus= $this->controller->_defineContextMenus();
		} else {
			$excludeList= $this->excludedActions;
			$class_methods= get_class_methods($this->controller);
			if ($this->controller->plugin) {
				$controller_methods= get_class_methods('AppController');
				$plugin= Inflector :: camelize($this->controller->plugin) . '/';
			} else {
				$controller_methods= get_class_methods('Controller');
				$plugin= false;
			}
			$class_methods= array_diff($class_methods, $controller_methods);
			$ContextMenu= array ();
			foreach ($class_methods as $method) {
				if (!in_array(low($method), $excludeList) && ($method {
					0 }
				<> '_')) {
					$admin= strpos($method, CAKE_ADMIN);
					if ($admin === false) {
						if (!stristr($method, 'controller')) {
							$url= $plugin ? '/' . $plugin : '/';
							if (Inflector :: camelize($this->controller->name) . '/' != Inflector :: camelize($plugin)) {
								$url .= Inflector :: camelize($this->controller->name) . '/';
							}
							$humanTitle= Inflector :: humanize(Inflector :: underscore($method));
							$method= Inflector :: underscore($method);
							$method[0]= low($method[0]);
							$url .= $method;
							$urlarray= array (
								'title' => $humanTitle,
								'url' => $url
							);
							if ($plugin) {
								$urlarray['plugin']= $plugin;
							}
							if (low($this->controller->action) == low($method)) {
								$urlarray['active']= true;
							}
							$ContextMenus['Actions']['data'][]= $urlarray;
						}
					}
				}
			}
		}
		if ($ContextMenus) {
			$this->__contextMenus= am($this->__contextMenus, $ContextMenus);
		}
	}
}
?>